#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@company:ZYF
@author:yu.qian
@file:objects.py
@time:2022/10/29
"""

import typing
from typing import Any
from .decorators import ResultData

_ObjectDictBase = typing.Dict[str, typing.Any]


class SuccessData(ResultData):
    """
    成功返回的结果
    """

    def __init__(self, data=None, msg=None, **kwargs):
        full_data = {**(dict(data) if isinstance(data, dict) else {'rows': data} if data is not None else {}), **kwargs}
        super(SuccessData, self).__init__(code=0, msg=msg or self.default_msg, **full_data)


class ObjectDict(_ObjectDictBase):
    """
    使字典的行为类似于对象，具有属性样式访问
    """

    def __getattr__(self, name: str) -> Any:
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    def __setattr__(self, name: str, value: Any) -> None:
        self[name] = value

if __name__ == '__main__':
    print(SuccessData(id=1, msg='用户注册成功'))